﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Boxman {
    // 小人
    class Man: Box {
        private Image mCurrentImage1;           // 当前图片1
        private Image mCurrentImage2;           // 当前图片2
        private readonly Control mGameBoard;    // 游戏区域
        private readonly Image mImageLeft1;     // 左图片1
        private readonly Image mImageLeft2;     // 左图片2
        private readonly Image mImageRight1;    // 右图片1
        private readonly Image mImageRight2;    // 右图片2
        private readonly Image mImageUp1;       // 上图片1
        private readonly Image mImageUp2;       // 上图片2
        private readonly Timer mTimer;          // 定时器

        /**
         * 构造方法。
         * @param back      背后对象
         * @param drawer    绘图者
         * @param gameBoard 游戏区域
         * @param type      类型
         * @param x, y      左上角坐标
         */
        public Man(BaseGameObject back, Graphics drawer, Control gameBoard, Type type, int x, int y): 
            base(back,drawer, type, x, y) {
            // 初始化成员
            mGameBoard = gameBoard;
            mTimer = new Timer();
            mTimer.Interval = Const.TIMER_INIT;
            mTimer.Tick += new EventHandler(Dancing);

            // 加载所有图片
            string imageFilePath;

            imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 2);
            mImageLeft1 = Image.FromFile(imageFilePath);

            imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 3);
            mImageLeft2 = Image.FromFile(imageFilePath);

            imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 4);
            mImageRight1 = Image.FromFile(imageFilePath);

            imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 5);
            mImageRight2 = Image.FromFile(imageFilePath);

            imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 6);
            mImageUp1 = Image.FromFile(imageFilePath);

            imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 7);
            mImageUp2 = Image.FromFile(imageFilePath);

            mCurrentImage1 = mImage;
            mCurrentImage2 = mImage2;

            // 开始跳舞
            mTimer.Start();
        }

        /**
         * 跳舞。
         * @param sender    事件发送者
         * @param e         事件相关信息  
         */
        private void Dancing(object sender, EventArgs e) {
            if (mCurrentImage == mCurrentImage1) {
                mCurrentImage = mCurrentImage2;
            } else {
                mCurrentImage = mCurrentImage1;
            }
            mDrawer.DrawImage(mCurrentImage, mX * Const.GRID_SIZE, mY * Const.GRID_SIZE, Const.GRID_SIZE, Const.GRID_SIZE);
            mGameBoard.Invalidate(false);
        }

        /**
         * 移动。
         * @param direction     方向
         * @param moveResult    移动结果
         * @return 被推动的箱子
         */
        public override BaseGameObject Move(Keys direction, ref MoveResult moveResult) {
            int destX = mX, destY = mY; // 新位置的坐标

            // 根据方向，计算新位置的坐标和图片
            switch (direction) {
                case Keys.Down:
                    destY++;
                    mCurrentImage1 = mImage;
                    mCurrentImage2 = mImage2;
                    break;
                case Keys.Left:
                    destX--;
                    mCurrentImage1 = mImageLeft1;
                    mCurrentImage2 = mImageLeft2;
                    break;
                case Keys.Right:
                    destX++;
                    mCurrentImage1 = mImageRight1;
                    mCurrentImage2 = mImageRight2;
                    break;
                case Keys.Up:
                    destY--;
                    mCurrentImage1 = mImageUp1;
                    mCurrentImage2 = mImageUp2;
                    break;
            }

            // 重新设置图片
            mCurrentImage = mCurrentImage1;

            // 先移动旁边的物体
            BaseGameObject box = Game.sBaseGameObjects[destY, destX].Move(direction, ref moveResult);
            if (moveResult == MoveResult.NO_MOVE) {
                return null;
            }

            // 移动小人
            base.Move(direction, ref moveResult);

            return box;
        }

        /**
         * 停止跳舞。
         */
        public void StopDancing() {
            mTimer.Stop();
        }
    }
}
